<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Common extends MY_Controller {

	function __construct() {
		parent::__construct();
	}

	
	function getToken(){
		$appid=ZJIPC_APP_ID;
		$appsecret=ZJIPC_APP_SECRET;
		$param=array(
			'appid'=>$appid,
		    'appsecret'=>$appsecret,
			'current_time' => time()
			);
		$param['sign']=$this->getSign($param, $appsecret);
		$result=$this->httpPost(ZJIPC_AUTH_URL.'Api/Token/getToken',json_encode($param));
		$result=json_decode($result,true);
		if($result['code']!=0){
			exit(retJson("获取token失败，错误信息：".$result['message'], false));
		}
		else{
			exit(retJson("", true,$result['token']));
		}
	}

	private function getSign($params, $appsecret) {
	    //按字典顺序排序
	    ksort($params);
	    $tmp = array();
	    foreach ($params as $key => $value) {
	      	$tmp[] = $key . $value;
	    }
	    $str = implode('', $tmp);
	    return sha1($str . $appsecret);
    }
    private function httpPost($url, $param) {
	        $ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL, $url);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	          'Content-Type: application/json; charset=utf-8',
	          'Content-Length: '.strlen($param)));
	        curl_setopt($ch, CURLOPT_POST, 1);
	        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	        $str = curl_exec($ch);
	        curl_close($ch);
	        return $str;
	}

	
}